﻿using Parylene_CVD.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Parylene_CVD.Commands;
using Parylene_CVD.Model.DModels;
using System.Collections.ObjectModel;
using Parylene_CVD.DAL;
using Parylene_CVD.Model;
using System.Windows.Threading;

namespace Parylene_CVD.ViewModel
{
    public class HomeViewModel : ViewModelBase
    {

        public ICommand? ToRunningCommand { get; }
        public ICommand? ToOpeningCommand { get; }
        public ICommand? ToClosingCommand { get; }
        public HomeViewModel(NavigationService SetViewNavigationService, NavigationService DeviceMonitorViewNavigationService,NavigationService HomeOpenViewNavigationService, NavigationService HomeCloseViewNavigationService) {
            ToOpeningCommand = new NavigateCommand(HomeOpenViewNavigationService);
            ToClosingCommand = new NavigateCommand(HomeCloseViewNavigationService);
            _deviceMonitorViewNavigationService = DeviceMonitorViewNavigationService;
            List<DeviceWithStatus> Devices1 = GlobalVariables.deviceWithStatuses;
            Devices = Devices1.Where(d => d.IsCoating).ToList();
            StartTimer();
            SetCommand = new NavigateCommand(SetViewNavigationService);
            ViewDetailsCommand = new RelayCommand(ViewDetail);
            TitlesSet();
        }

        /// <summary>
        /// 开启定时器读取设备状态
        /// </summary>
        public void StartTimer()
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += SetDevices;
            timer.Start();
        }
        public void SetDevices(object sender, EventArgs e)
        {
            List<DeviceWithStatus>  Devices1 = GlobalVariables.deviceWithStatuses;
            Devices = Devices1.Where(d => d.IsCoating).ToList();

        }
        private readonly NavigationService _deviceMonitorViewNavigationService;

        /// <summary>
        /// 页面按钮绑定命令
        /// </summary>
        public ICommand SetCommand { get; }
        public ICommand ViewDetailsCommand { get; }
        private void ViewDetail(object parameter)
        {
            string deviceName = parameter as string;
            if (!string.IsNullOrEmpty(deviceName))
            {
                GlobalVariables.CurDeviceName = deviceName;
                _deviceMonitorViewNavigationService.Navigate();  // 跳转到监视界面
            }
        }


        /// <summary>
        /// 页面绑定数据
        /// </summary>
        private List<DeviceWithStatus>? devices;
        public List<DeviceWithStatus>? Devices
        {
            get { return devices; }
            set { devices = value; OnPropertyChanged(nameof(Devices)); }
        }



        //页面显示文本定义与初始化:
        public string? Title { get; set; }
        public string? SetTitle { get; set; }
        private void TitlesSet()
        {
            Title = "Parylene CVD真空镀膜远程监视系统";
            SetTitle = "设置";

        }
    }
}
